package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.LbsTrackRpcService;
import com.hunttown.mes.rpc.domain.LbsTrackDTO;
import com.hunttown.mes.rpc.domain.query.LbsTrackDTOQuery;
import com.hunttown.mes.service.LbsTrackService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import static com.hunttown.mes.convert.LbsTrackConverts.convert;

/**
 * created by wangjunfu on 2021-07-05 11:06:24
 */
@Service
public class LbsTrackRpcServiceImpl implements LbsTrackRpcService {

    private final Logger logger = LoggerFactory.getLogger(LbsTrackRpcServiceImpl.class);

    private final LbsTrackService lbsTrackService;

    @Autowired
    public LbsTrackRpcServiceImpl(LbsTrackService lbsTrackService) {
        this.lbsTrackService = lbsTrackService;
    }

    @Override
    public LbsTrackDTO insert(LbsTrackDTO lbsTrackDTO) {
        try {
            return convert(lbsTrackService.insert(convert(lbsTrackDTO)));
        } catch (Exception e) {
            logger.error("LbsTrack create failed.", e);
            throw new RuntimeException("LbsTrack create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(LbsTrackDTO lbsTrackDTO) {
        try {
            return lbsTrackService.updateInfoById(convert(lbsTrackDTO));
        } catch (Exception e) {
            logger.error("LbsTrack update by id failed.", e);
            throw new RuntimeException("LbsTrack update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(LbsTrackDTOQuery lbsTrackDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTrackDTOQuery.getW_id() != null && lbsTrackDTOQuery.getW_id() > 0) {
                query.put("w_id", lbsTrackDTOQuery.getW_id());
            }

            if (lbsTrackDTOQuery.getIds() != null && !lbsTrackDTOQuery.getIds().equals("")) {
                query.put("ids", lbsTrackDTOQuery.getIds());
            }

            if (lbsTrackDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsTrackDTOQuery.getStopFlag());
            }

            if (lbsTrackDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTrackDTOQuery.getDeleteFlag());
            }

            return lbsTrackService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("LbsTrack update by query failed.", e);
            throw new RuntimeException("LbsTrack update by query failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTrackDTO selectById(Integer id) {
        try {
            return convert(lbsTrackService.selectById(id));
        } catch (Exception e) {
            logger.error("LbsTrack select by id failed.", e);
            throw new RuntimeException("LbsTrack select by id failed, Please order your code.", e);
        }
    }

    @Override
    public LbsTrackDTO selectObjByQuery(LbsTrackDTOQuery lbsTrackDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (lbsTrackDTOQuery.getEntId() != null) {
                query.put("entId", lbsTrackDTOQuery.getEntId());
            }

            if (lbsTrackDTOQuery.getTrackCode() != null) {
                query.put("trackCode", lbsTrackDTOQuery.getTrackCode());
            }

            if (lbsTrackDTOQuery.getTerminalId() != null) {
                query.put("terminalId", lbsTrackDTOQuery.getTerminalId());
            }

            if (StringUtils.isNotBlank(lbsTrackDTOQuery.getShowDate())) {
                query.put("showDate", lbsTrackDTOQuery.getShowDate());
            }

            if (lbsTrackDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", lbsTrackDTOQuery.getStopFlag());
            }

            if (lbsTrackDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", lbsTrackDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(lbsTrackDTOQuery.getOrderBy())) {
                query.put("orderBy", lbsTrackDTOQuery.getOrderBy());
            }

            return convert(lbsTrackService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("LbsTrack select obj by query failed.", e);
            throw new RuntimeException("LbsTrack select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<LbsTrackDTO> getListForPage(LbsTrackDTOQuery lbsTrackDTOQuery) {
        Query query = Query.page(lbsTrackDTOQuery.getPn(), lbsTrackDTOQuery.getPageSize());

        if (lbsTrackDTOQuery.getId() != null && lbsTrackDTOQuery.getId() > 0) {
            query.put("id", lbsTrackDTOQuery.getId());
        }

        if (lbsTrackDTOQuery.getIds() != null && !lbsTrackDTOQuery.getIds().equals("")) {
            query.put("ids", lbsTrackDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(lbsTrackDTOQuery.getLikeName())) {
            query.put("likeName", lbsTrackDTOQuery.getLikeName());
        }

        if (lbsTrackDTOQuery.getEntId() != null) {
            query.put("entId", lbsTrackDTOQuery.getEntId());
        }

        if (lbsTrackDTOQuery.getServiceId() != null) {
            query.put("serviceId", lbsTrackDTOQuery.getServiceId());
        }

        if (lbsTrackDTOQuery.getTerminalId() != null) {
            query.put("terminalId", lbsTrackDTOQuery.getTerminalId());
        }

        if (StringUtils.isNotBlank(lbsTrackDTOQuery.getShowDate())) {
            query.put("showDate", lbsTrackDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(lbsTrackDTOQuery.getStartDate())) {
            query.put("startDate", lbsTrackDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(lbsTrackDTOQuery.getEndDate())) {
            query.put("endDate", lbsTrackDTOQuery.getEndDate());
        }

        if (lbsTrackDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", lbsTrackDTOQuery.getStopFlag());
        }

        if (lbsTrackDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", lbsTrackDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(lbsTrackDTOQuery.getOrderBy())) {
            query.put("orderBy", lbsTrackDTOQuery.getOrderBy());
        }

        List<LbsTrackDTO> list = convert(lbsTrackService.selectByQuery(query));
        Page<LbsTrackDTO> page = Page.from(lbsTrackDTOQuery.getPn(), lbsTrackDTOQuery.getPageSize(), list);
        page.setTotalItems(lbsTrackService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return lbsTrackService.deleteById(id);
        } catch (Exception e) {
            logger.error("LbsTrack delete failed.", e);
            throw new RuntimeException("LbsTrack delete failed, Please order your code.", e);
        }
    }

    @Override
    public Map<String, Integer> getTraceKeyFromCache(String terminalCode, Integer clientType, Integer userType, Integer entId) {
        try {
            return lbsTrackService.getTraceKeyFromCache(terminalCode, clientType, userType, entId);
        } catch (Exception e) {
            logger.error("LbsTrack get trace key failed.", e);
            throw new RuntimeException("LbsTrack get trace key failed, Please order your code.", e);
        }
    }

    @Override
    public String packageTrackCode(Integer clientType, Integer userType, Integer onlyId) {
        try {
            return lbsTrackService.packageTrackCode(clientType, userType, onlyId);
        } catch (Exception e) {
            logger.error("LbsTrack package trace code failed.", e);
            throw new RuntimeException("LbsTrack package trace code failed, Please order your code.", e);
        }
    }
}